package com.jerry.walking.module.home.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jerry.walking.R;
import com.jerry.walking.base.BaseActivity;
import com.jerry.walking.base.BaseFragment;
import com.jerry.walking.module.activityMe.fragment.ActivityMeFragment;
import com.jerry.walking.module.activityPage.fragment.ActivityHomeFragment;
import com.jerry.walking.module.activityPath.fragment.ActivityPathFragment;
import com.jerry.walking.module.activityTravel.fragment.ActivityTravelFragment;
import com.jerry.walking.utils.EventMessage;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import greendao.LoreDetailTableDao;

/**
 *          主界面
 */
public class MainActivity extends BaseActivity
{
    private FragmentManager manager;
    private LoreDetailTableDao dao;
    private boolean flag = false;

    //存放fragment集合
    private Map<Integer, Fragment> fragmentMap;

    //保存上个界面来优化fragment
    private Fragment lastFragment;

    //fragment放置界面
    @BindView(R.id.fr_main)
    FrameLayout frMain;

    @BindView(R.id.rg_categry)
    RadioGroup radioGroup;

    @Override
    protected int getLayoutId()
    {
        return R.layout.activity_main;
    }

    /**
     * 初始化视图
     */
    @Override
    protected void initViews()
    {
        manager = getSupportFragmentManager();

        FragmentTransaction transaction = manager.beginTransaction();
        Fragment fragment = BaseFragment.newInstance(ActivityTravelFragment.class,null);
        transaction.add(R.id.fr_main,fragment).commit();

        //初始化fragment集合
        fragmentMap = new HashMap<>();

        //rg点击事件
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction transaction = manager.beginTransaction();

                Fragment fragment = null;
                if (fragmentMap.containsKey(checkedId)) {
                    if (lastFragment != null) {
                        transaction.hide(lastFragment);
                    }
                    fragment = fragmentMap.get(checkedId);
                    transaction.show(fragment);
                    lastFragment = fragment;

                } else {
                    switch (checkedId){
                        case R.id.rb_activity:
                            fragment = BaseFragment.newInstance(ActivityHomeFragment.class, null);
                            break;

                        case R.id.rb_youji:
                            fragment = BaseFragment.newInstance(ActivityTravelFragment.class, null);
                            break;

                        case R.id.rb_upgrade_photo:
                            initAdd();
                            flag = true;
                            break;

                        case R.id.rb_road:
                            fragment = BaseFragment.newInstance(ActivityPathFragment.class, null);
                            break;

                        case R.id.rb_my:
                            fragment = BaseFragment.newInstance(ActivityMeFragment.class, null);
                            break;
                    }
                    if (!flag) {
                        transaction.add(R.id.fr_main, fragment);
                        fragmentMap.put(checkedId, fragment);
                        if (lastFragment != null) {
                            transaction.hide(lastFragment);
                        }
                        lastFragment = fragment;
                    }
                }
                if (!flag) {
                    transaction.commit();
                }
                flag = false;
            }
        });

    }

    /**
     * 加载数据
     */
    @Override
    protected void loadData()
    {

    }

    /**
     * 获取具体的结果
     *
     * @param message
     */
    public void postEventResult(EventMessage message)
    {

    }

    /**
     * 点击中间加号按钮
     * 用户登录
     */
    private void initAdd() {
        //得到视图
        View view = View.inflate(this, R.layout.layout_main_add, null);
        TextView textView1 = (TextView) view.findViewById(R.id.tv_add_xinlang);
        TextView textView2 = (TextView) view.findViewById(R.id.tv_add_weixin);
        TextView textView3 = (TextView) view.findViewById(R.id.tv_add_qq);

        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO  新浪登录
                Log.d("--->", "新浪");
            }
        });
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO  微信登录
                Log.d("--->", "微信");
            }
        });
        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO  qq登录
                Log.d("--->", "qq");
            }
        });

        AlertDialog dialog = new AlertDialog.Builder(this).setTitle("登录")
                .setView(view)

                .create();
        dialog.show();
    }


    /**
     * 延时退出效果
     */
    private boolean back = true;
    private long last;

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        if (back) {
            Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
            back = false;
            last = System.currentTimeMillis();
        } else {
            if (System.currentTimeMillis() - last < 2000) {
                finish();
            } else {
                Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();

                last = System.currentTimeMillis();
            }
        }

    }
}
